package com.project.util;

import com.project.dao.IUserDao;

import java.io.File;

public class DaoScanFactory {
    private static File[] dirFile = new File("C:\\javaseHigh\\src\\com\\project\\dao\\impl").listFiles();

    public static Object getDao(Class daoInter){
        for(File f : dirFile){
           if(f.getName().endsWith(".java")){
               String classPath = "com.project.dao.impl."+f.getName();
               classPath = classPath.substring(0,classPath.length() - 5);

               //加载类
               try {
                   Class c = Class.forName(classPath);
                   //判断该类是否匹配指定的接口
                   if(daoInter.isAssignableFrom(c)){
                       return c.getConstructor().newInstance();
                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(DaoScanFactory.getDao(IUserDao.class));
    }
}
